import axios from 'axios'
import qs from 'qs'
import config from '../config'

axios.interceptors.request.use(options => {
  const p = {
    ...options,
    url: config.api.baseURL + options.url,
    data: qs.stringify({
      ...options.data,
      sign: localStorage.getItem('token')
    }),
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  }
  return p
}, error => Promise.reject(error))

axios.interceptors.response.use(response => {
  const { Status } = response.data
  switch (Status) {
    // 未登录
    case 551:
      localStorage.removeItem('token')
      break
    default:
      break
  }
  return response
}, error => {
  Promise.reject(error)
})
